//
//  ByViewPhysicalBuyHeader.m
//  jinpuyiyuan_ios
//
//  Created by pro on 2017/12/13.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewPhysicalBuyHeader.h"


@interface ByViewPhysicalBuyHeader()
@property (nonatomic, strong) UIView *contentView;
//@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) NSMutableArray *resetAry;     // 重组数组
@property (nonatomic, assign) NSInteger preTag;
@property (nonatomic, assign) NSInteger more_index;         // 记录第几个开始第二行
@end

@implementation ByViewPhysicalBuyHeader
- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
        _preTag = 10;
    }
    return self;
}
#pragma mark - getter
- (NSMutableArray *)resetAry{
    if (!_resetAry) {
        _resetAry = [NSMutableArray array];
    }
    return _resetAry;
}
#pragma mark -
- (void)setItems:(NSMutableArray *)items{
    _items = items;
    [self createViews:_items];
}

- (void)createViews:(NSMutableArray *)parts{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    self.contentView = [[UIView alloc] init];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
    }];
    self.contentView.backgroundColor = BY_TABLE_COLOR;
    
    CGFloat width,margin = 10;
    CGFloat x_offset = 10;
    CGFloat more_width = [@"更多>" getSizeOfString:13].width + 10;
    for (int i = 0; i < parts.count ; i ++) {
        ByModelPhysicalBuyCate *model = parts[i];
        width = [model.name getSizeOfString:14].width + 30;
        if (x_offset + width > SCREEN_WIDTH - more_width - 10){
            _more_index = i + 10;
            UIButton *moreButton = [[UIButton alloc] init];
            [self.contentView addSubview:moreButton];
            [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@(-10));
                make.top.equalTo(@(10));
                make.size.mas_equalTo(CGSizeMake(more_width, 30));
            }];
            moreButton.titleLabel.font = FONT(13);
            [moreButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [moreButton setTitle:@"更多>" forState:UIControlStateNormal];
            [moreButton addTarget:self action:@selector(moreAction) forControlEvents:UIControlEventTouchUpInside];
            break;
        }else{
            UIButton *button = [[UIButton alloc] init];
            [self.contentView addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@(x_offset));
                make.top.equalTo(@(10));
                make.size.mas_equalTo(CGSizeMake(width, 30));
            }];
            [button setTitle:model.name forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = FONT(14);
            button.backgroundColor = [UIColor whiteColor];
            button.layer.cornerRadius = 30 / 2;
            [button addTarget:self action:@selector(lessButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 10 + i;
            if (_preTag == button.tag) {
                [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
            }else{
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                
            }
    
        }
        x_offset += width + margin;
    }
    
//    [self createBgView:YES];
}

- (void)createAllViews:(NSMutableArray *)allItems{
    for (UIView *subview in self.subviews) {
        [subview removeFromSuperview];
    }
    self.contentView = [[UIView alloc] init];
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = BY_TABLE_COLOR;
    CGFloat width,margin = 10;
    CGFloat x_offset = 10;
    CGFloat y_offset = 10;
    for (int i = 0; i < _items.count ; i ++) {
        ByModelPhysicalBuyCate *model = _items[i];
        width = [model.name getSizeOfString:14].width + 30;
        UIButton *button = [[UIButton alloc] init];
        [self.contentView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(x_offset));
            make.top.equalTo(@(y_offset));
            make.size.mas_equalTo(CGSizeMake(width, 30));
        }];
        [button setTitle:model.name forState:UIControlStateNormal];
        button.titleLabel.font = FONT(14);
        button.backgroundColor = [UIColor whiteColor];
        button.layer.cornerRadius = 30 / 2;
        button.tag = 10 + i;
        [button addTarget:self action:@selector(allButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        x_offset += width + margin;
        if (x_offset + width > SCREEN_WIDTH - 10) {
            x_offset = 10;
            y_offset += 30 + margin;
        }
        if (_preTag == button.tag) {
            [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(allButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, [self getHeightHeader]));
    }];
//    [self createBgView:NO];
    
}

//- (void)createBgView:(BOOL)isHiden{
//    self.bgView = [[UIView alloc] init];
//    [self addSubview:self.bgView];
//    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@(0));
//        make.right.equalTo(@(0));
//        make.top.equalTo(self.contentView.mas_bottom);
//        make.bottom.equalTo(@(0));
//    }];
//    self.bgView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
//    self.bgView.userInteractionEnabled = YES;
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBgView)];
//    [self.bgView addGestureRecognizer:tap];
//    self.bgView.hidden = isHiden;
//}

// 更多
- (void)moreAction{
    [self createAllViews:_items];
    if (self.showBlock) {
        self.showBlock();
    }
//    self.bgView.hidden = NO;
}

// button 点击事件
- (void)allButtonAction:(UIButton *)button{
    if (_preTag == button.tag) {
        return;
    }
    if (button.tag >= _more_index) {
        ByModelPhysicalBuyCate *model = _items[button.tag - 10];
        ByModelPhysicalBuyCate *origin_model = _items[1];
        [_items replaceObjectAtIndex:1 withObject:model];
        [_items replaceObjectAtIndex:button.tag - 10 withObject:origin_model];
        _preTag = 11;
    }else{
        _preTag = button.tag;
    }
    [self createViews:_items];
    if (self.clickBlock) {
        ByModelPhysicalBuyCate *model = _items[button.tag - 10];
        self.clickBlock(model);
    }
    if (self.buttonBlock) {
        self.buttonBlock(_items);
    }
    
}

// 部分 button点击事件
- (void)lessButtonAction:(UIButton *)button{
    if (_preTag == button.tag) {
        return;
    }
    UIButton *preButton = [self.contentView viewWithTag:_preTag];
    [preButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:BY_THEME_COLOR forState:UIControlStateNormal];
    _preTag = button.tag;
    if (self.clickBlock) {
        ByModelPhysicalBuyCate *model = _items[button.tag - 10];
        self.clickBlock(model);
    }
}

#pragma mark - 计算高度
- (CGFloat)getHeightHeader{
    CGFloat width,margin = 10;
    CGFloat x_offset = 10;
    CGFloat y_offset = 10;
    for (int i = 0; i < _items.count ; i ++) {
        ByModelPhysicalBuyCate *model = _items[i];
        width = [model.name getSizeOfString:14].width + 30;
        x_offset += width + margin;
        if (x_offset + width > SCREEN_WIDTH - 10) {
            x_offset = 10;
            y_offset += 30 + margin;
        }
    }
    
    return y_offset  + 30 + margin;
}
@end
